package softwaredepartment.zxit.com.pt_3280cinandroid;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;
import android.view.Window;
import android.widget.LinearLayout;

import com.socks.library.KLog;

import softwaredepartment.zxit.com.pt_3280cinandroid.Service.IsCallMeService;

public class FunctionsActivity extends Activity implements View.OnClickListener {
    private LinearLayout ic_talking;
    private LinearLayout ic_broadcast;
    private LinearLayout ic_music;
    private LinearLayout ic_net_center;
    private LinearLayout ic_distract;
    private LinearLayout ic_setup;
    private LinearLayout ic_about;
    private Intent intent ;
    private IsCallMeService i$CallMe;
    private ServiceConnection sc = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            i$CallMe = ((IsCallMeService.I$CallYouBinder) iBinder).getService();
        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {
            i$CallMe = null;
        }
    };

    private void bindServiceConnection() {
        Intent intent = new Intent(FunctionsActivity.this, IsCallMeService.class);
        startService(intent);
        bindService(intent, sc, this.BIND_AUTO_CREATE);
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_functions);
        bindServiceConnection();
        KLog.i("LOG","bindServiceConnection()");
        init();
        KLog.i("LOG","OnCreate()");

        IntentFilter filter = new IntentFilter("com.zxIT.softDepartment.broadcast.beCall");
        BroadcastBeCalled receiver = new BroadcastBeCalled();
        registerReceiver(receiver, filter);

    }

    @Override
    protected void onResume() {
        super.onResume();
        KLog.i("LOG","onResume()");
    }

    private void init() {
        ic_talking = (LinearLayout) findViewById(R.id.ic_talking);
        ic_broadcast = (LinearLayout) findViewById(R.id.ic_broadcast);
        ic_music = (LinearLayout) findViewById(R.id.ic_music);
        ic_net_center = (LinearLayout) findViewById(R.id.ic_net_center);
        ic_distract = (LinearLayout) findViewById(R.id.ic_distract);
        ic_setup = (LinearLayout) findViewById(R.id.ic_setup);
        ic_about = (LinearLayout) findViewById(R.id.ic_about);


        ic_talking.setOnClickListener(this);
        ic_broadcast.setOnClickListener(this);
        ic_music.setOnClickListener(this);
        ic_net_center.setOnClickListener(this);
        ic_distract.setOnClickListener(this);
        ic_setup.setOnClickListener(this);
        ic_about.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ic_talking:
                intent = new Intent(FunctionsActivity.this,EachOtherTalkActivity.class);
                startActivity(intent);
                break;
            case R.id.ic_broadcast:
                intent = new Intent(FunctionsActivity.this,BroadcastTalkingActivity.class);
                startActivity(intent);
                break;
            case R.id.ic_music:
                intent = new Intent(FunctionsActivity.this,PlayMusicActivity.class);
                startActivity(intent);
                break;
            case R.id.ic_net_center:

                break;
            case R.id.ic_distract:

                break;
            case R.id.ic_setup:

                break;
            case R.id.ic_about:
                intent = new Intent(FunctionsActivity.this,AboutActivity.class);
                startActivity(intent);
                break;
            case R.id.bt_back:
                finish();
                break;
            default :
                break;
        }
    }

    public class BroadcastBeCalled extends BroadcastReceiver{

        @Override
        public void onReceive(Context context, Intent intent) {

            KLog.i("LOG",intent.getAction());
            intent = new Intent(FunctionsActivity.this,CallMeActivity.class);
            startActivity(intent);
        }
    }
}
